package com.wp.controller.school;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.wp.common.CURDUtil;
import com.wp.common.RequestUtil;
import com.wp.dto.AjaxDTO;

import net.sf.json.JSONArray;

@Controller
public class IntroduceController{
	private static Logger log = Logger.getLogger(JuniorController.class);
	
	//学校介绍
	@RequestMapping("school/introduce.do")
	public ModelAndView profile(HttpServletRequest req,Model model){
		return new ModelAndView("school/introduce/profile");
	}
	
	//年级信息
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping("school/introduce/grade.do")
	public ModelAndView grade(HttpServletRequest req,Model model){
		Map<String,Object> params = RequestUtil.getParams(req);
		try{
			String schoolName = (String)CURDUtil.queryObject("school.querySchoolNameByPosition", params);
			List<Map> gradeList = CURDUtil.queryList("school.querySeniorGrade", params);
			model.addAttribute("schoolName", schoolName);
			model.addAttribute("gradeList", gradeList);
		}catch(Exception e){
			log.error("queryError:"+e.getMessage());
		}
		model.addAllAttributes(params);
		return new ModelAndView("school/introduce/grade");
	}
	
	//查询年级信息的详情(班级信息)
	@SuppressWarnings({ "unchecked" })
	@RequestMapping(value="school/introduce/queryDetailGradeInfo.do",produces="application/json; charset=utf-8")
	public @ResponseBody String queryGradeDetailInfo(HttpServletRequest req,Model model){
		Map<String,Object> params = RequestUtil.getParams(req);
		List<Map<String,String>> gradeList = new ArrayList<Map<String,String>>();
		String resString = "";
		try{
			gradeList = CURDUtil.queryList("school.queryGradeDetailInfo", params);
			resString = JSONArray.fromObject(gradeList).toString();
		}catch(Exception e){
			log.error("Error-queryGradeDetailInfo:"+e.getMessage());
		}
		return resString;
//		return gradeList.get(0);
	}
	
	//班级信息查询学生
	@RequestMapping("school/introduce/toQueryStudent.do")
	public ModelAndView classManag(HttpServletRequest req,Model model){
		Map<String,Object> params = RequestUtil.getParams(req);
		log.info(params);
		//查询数据用于页面展示选择
		try{
			String schoolName = (String)CURDUtil.queryObject("school.querySchoolNameByPosition", params);
			List<Map> gradeList = CURDUtil.queryList("school.queryExistSeniorGrade", params);
			if(null != gradeList && gradeList.size()>0){
				params.put("gradeLevel",gradeList.get(0).get("dicCode"));
				List<AjaxDTO> classList = CURDUtil.queryList("school.queryExistSeniorClass", params);
				model.addAttribute("classList", classList);
			}
			model.addAttribute("schoolName",schoolName);
			model.addAttribute("gradeList",gradeList);
		}catch(Exception e){
			log.error("queryError:"+e.getMessage());
		}
		model.addAllAttributes(params);
		
		return new ModelAndView("school/introduce/classManag");
	}
	
	//查询班级内学生的信息(班级信息)
	@SuppressWarnings({ "unchecked" })
	@RequestMapping(value="school/introduce/queryClassStudentInfo.do",produces="application/json; charset=utf-8")
	public @ResponseBody String queryClassDetailInfo(HttpServletRequest req,Model model){
		Map<String,Object> params = RequestUtil.getParams(req);
		List<Map<String,String>> gradeList = new ArrayList<Map<String,String>>();
		String resString = "";
		try{
			gradeList = CURDUtil.queryList("school.queryClassDetailInfo", params);
			resString = JSONArray.fromObject(gradeList).toString();
		}catch(Exception e){
			log.error("Error-queryGradeDetailInfo:"+e.getMessage());
		}
		return resString;
//		return gradeList.get(0);
	}
	
	//添加班级信息
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping("school/introduce/addClass.do")
	public ModelAndView toAddClass(HttpServletRequest req,Model model){
		Map<String,Object> params = RequestUtil.getParams(req);
		log.info(params);
		//查询数据用于页面展示选择
		try{
			List<Map> gradeList = CURDUtil.queryList("school.querySeniorGrade", params);
			List<Map> majorList = CURDUtil.queryList("school.querySeniorMajorType", params);
			model.addAttribute("gradeList",gradeList);
			model.addAttribute("majorList",majorList);
		}catch(Exception e){
			log.error("queryError:"+e.getMessage());
		}
		model.addAllAttributes(params);
		return new ModelAndView("school/introduce/addClass");
	}
	
	//新增年级及班级信息
	@SuppressWarnings("unchecked")
	@RequestMapping("school/introduce/saveNewGradeInfo.do")
	public @ResponseBody String saveNewClassInfo(HttpServletRequest req,Model model){
		Map<String,Object> params = RequestUtil.getParams(req);
		String res="01";//01-保存成功 02-班级已经存在 03-保存失败
		log.info(params);
		//查询数据用于页面展示选择
		try{
			int flag = (Integer)CURDUtil.queryObject("school.queryGradeClassIsExists", params);
			if(flag > 0){
				res = "02";
			}else{
				CURDUtil.insert("school.saveNewGradeInfo", params);
			}
		}catch(Exception e){
			res = "03";
			log.error("insertFailed:"+e.getMessage());
		}
		return res;
	}
	
	//班级内新增学生信息
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping("school/introduce/addStudent.do")
	public ModelAndView toAddStudents(HttpServletRequest req,Model model){
		Map<String,Object> params = RequestUtil.getParams(req);
		log.info(params);
		//查询数据用于页面展示选择
		try{
			String schoolName = (String)CURDUtil.queryObject("school.querySchoolNameByPosition", params);
			List<Map> gradeList = CURDUtil.queryList("school.queryExistSeniorGrade", params);
			if(null != gradeList && gradeList.size()>0){
				params.put("gradeLevel",gradeList.get(0).get("dicCode"));
				List<AjaxDTO> classList = CURDUtil.queryList("school.queryExistSeniorClass", params);
				model.addAttribute("classList", classList);
			}
			model.addAttribute("schoolName",schoolName);
			model.addAttribute("gradeList",gradeList);
			
		}catch(Exception e){
			log.error("queryError:"+e.getMessage());
		}
		model.addAllAttributes(params);
		return new ModelAndView("school/introduce/addStudent");
	}
	
	//保存班级内学生信息
	@SuppressWarnings("unchecked")
	@RequestMapping("school/introduce/SaveStudentInfo.do")
	public @ResponseBody String toSaveStudentInfo(HttpServletRequest req,Model model){
		Map<String,Object> params = RequestUtil.getParams(req);
		String res="01";//01-保存成功 02-学生信息已经存在 03-保存失败
		log.info(params);
		//查询数据用于页面展示选择
		try{
			int flag = (Integer)CURDUtil.queryObject("school.queryStudentClassIsExists", params);
			if(flag > 0){
				res = "02";
			}else{
				CURDUtil.insert("school.saveNewStudentInfo", params);
			}
		}catch(Exception e){
			res = "03";
			log.error("insertFailed:"+e.getMessage());
		}
		return res;
	}
}
